﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using ClassLibrary.Utilities;

public partial class Client_LostPassword : System.Web.UI.Page
{
    TblUserAccountDAO userDAO = new TblUserAccountDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void cmdSend_Click(object sender, ImageClickEventArgs e)
    {

        try
        {
            TblUserAccount user = new TblUserAccount();
            string newPwd = string.Empty;

            if (!userDAO.CheckExits(txtUsername.Text, 1))
            {
                lblMessage.Text = "This email is not registered!";

            }
            else
            {
                lblMessage.Text = "Password will be sent to your mail.<br/><br/>Please check your email <b><u>" + txtUsername.Text.Trim() + "</u></b>.";
                lblMessage.ForeColor = System.Drawing.Color.Blue;
                pnl.Visible = false;

                EmailService emailSrv = new EmailService();

                //Send to specified department                
                user = userDAO.GetAll(txtUsername.Text);
                newPwd = Global.randomString(6);
                //DataTable dt = userDAO.ResetPass();
                //foreach (DataRow dr in dt.Rows)
                //{
                //    newPwd = dr["newpass"].ToString();
                //}

                // send mail
                emailSrv.SendChangePasswordEmail(user, Server.MapPath(Global.RESET_PASSWORD), newPwd);
                user.Password = Encryption.EncryptToString(newPwd);
                userDAO.Insert_Update(user, 4);
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
       
    }
}
